package org.lee.myshiroredisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.authc.UnknownAccountException;
import org.lee.myshiroredisplus.entity.AccountProfile;
import org.lee.myshiroredisplus.entity.User;
import org.lee.myshiroredisplus.mapper.UserMapper;
import org.lee.myshiroredisplus.service.UserService;
import org.springframework.stereotype.Service;

/**
 * @author jackielee
 * @version 1.0
 * @description UserServiceImpl
 * @date 2021/4/9
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public AccountProfile login(String username, String password) {

        User user = baseMapper.selectOne(new QueryWrapper<User>().eq("username", username).eq("password", password));

        if(user == null){
            throw new UnknownAccountException("用户名不存在或密码错误"); // 抛出shiro异常，方便通知用户错误信息
        }

        AccountProfile profile = new AccountProfile();
        profile.setId(user.getId());
        profile.setUserName(user.getUsername());
        profile.setSign("欢迎您"+user.getUsername());

        return profile;
    }
}
